﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.IO;
using System.Xml.Linq;
using Microsoft.JScript;
using SGD;

namespace WindowsFormsApplication1
{
    public partial class ParticleED : Form
    {
        float fElapsedTime = 0;
        float fLastTime = 0;
        Stopwatch stopwatch = new Stopwatch();

        Microsoft.DirectX.Direct3D.Blend Current_SourceBlend = 0;
        Microsoft.DirectX.Direct3D.Blend Current_DestBlend = 0;
        Microsoft.DirectX.Direct3D.Blend Original_SourceBlend = 0;
        Microsoft.DirectX.Direct3D.Blend Original_DestBlend = 0;

        bool once = true;
        bool something_happened = false;

        decimal EmitterX = 200;
        decimal EmitterY = 250;
        decimal OriginalX = 200;
        decimal OriginalY = 250;

        double EmitterAnimation_Degree = 0;
        int EmitterAnimation_Type = 0;
        decimal EmitterAnimationVelX = 0;
        decimal EmitterAnimationVelY = 0;

        decimal BG_Red = 0;
        decimal BG_Green = 0;
        decimal BG_Blue = 0;

        int Start_Alpha = 255;
        int Start_Red = 0;
        int Start_Green = 255;
        int Start_Blue = 0;

        int End_Alpha = 255;
        int End_Red = 0;
        int End_Green = 0;
        int End_Blue = 0;

        float Start_Scale = 0.25f;
        float End_Scale = 0.75f;

        decimal Particle_Quanity = 64;

        decimal ParticleLife_Min = 100;
        decimal ParticleLife_Max = 1000;

        decimal ParticleVel_MinX = -10;
        decimal ParticleVel_MaxX = 10;

        decimal ParticleAccel_MinX = -1;
        decimal ParticleAccel_MaxX = 1;

        decimal ParticleVel_MinY = -10;
        decimal ParticleVel_MaxY = 10;

        decimal ParticleAccel_MinY = -1;
        decimal ParticleAccel_MaxY = 1;

        int Max_Particles = 5000;

        int ImageID = -1;
        string ParticleFileName = null;

        int EmitterType = 0;
        int EmitterSize = 10;

        struct Particle
        {
            private decimal x;

            public decimal X
            {
                get { return x; }
                set { x = value; }
            }

            private decimal y;
            public decimal Y
            {
                get { return y; }
                set { y = value; }
            }

            public Color[] Color_Array;
            public Color Current_Color;
            public decimal Current_Life;
            public decimal Max_Life;
            public decimal Current_Scale;
            public decimal End_Scale;
            public decimal Start_Scale;
            public decimal Current_VelocityX;
            public decimal Current_VelocityY;
            public decimal Current_AccelerationX;
            public decimal Current_AccelerationY;
        };

        List<Particle> Particles = new List<Particle>();

        ManagedDirect3D Direct3D = ManagedDirect3D.Instance; // = ManagedDirect3D.Instance;
        ManagedTextureManager TextureManager = ManagedTextureManager.Instance;

        public ParticleED()
        {
            InitializeComponent();
            //Direct3D = ManagedDirect3D.Instance;
            Direct3D.InitManagedDirect3D(splitContainer1.Panel1, false);
            TextureManager.InitManagedTextureManager(Direct3D.Device, Direct3D.Sprite);

            Original_SourceBlend = Microsoft.DirectX.Direct3D.Blend.InvSourceAlpha;
            Original_DestBlend = Microsoft.DirectX.Direct3D.Blend.SourceAlpha;
            

            Random rand = new Random();

            

            for (int i = 0; i < Particle_Quanity; ++i)
            {
                Particle temp_particle = new Particle();
                temp_particle.X = rand.Next(10)+EmitterX;
                temp_particle.Y = rand.Next(10) + EmitterY;
                temp_particle.Color_Array = BlendColors(Color.FromArgb((int)End_Alpha, (int)End_Red, (int)End_Green, (int)End_Blue),
                    Color.FromArgb((int)Start_Alpha, (int)Start_Red, (int)Start_Green, (int)Start_Blue),100);
                temp_particle.Current_Color = temp_particle.Color_Array[99];
                temp_particle.Current_Scale = temp_particle.Start_Scale = (decimal)Start_Scale;
                temp_particle.End_Scale = (decimal)End_Scale;
                temp_particle.Current_Life = temp_particle.Max_Life = rand.Next((int)(ParticleLife_Max - ParticleLife_Min)) + ParticleLife_Min;
                temp_particle.Current_VelocityX = rand.Next((int)(ParticleVel_MaxX - ParticleVel_MinX)) + ParticleVel_MinX;
                temp_particle.Current_VelocityY = rand.Next((int)(ParticleVel_MaxY - ParticleVel_MinY)) + ParticleVel_MinY;
                temp_particle.Current_AccelerationX = rand.Next((int)(ParticleAccel_MaxX - ParticleAccel_MinX)) + ParticleAccel_MinX;
                temp_particle.Current_AccelerationY = rand.Next((int)(ParticleAccel_MaxY - ParticleAccel_MinY)) + ParticleAccel_MinY;
                Particles.Add(temp_particle);
            }
            comboBox1.SelectedIndex = 11;
            comboBox2.SelectedIndex = 8;

            //ImageID = TextureManager.LoadTexture("C:\\Users\\Anthony\\Documents\\SGP\\WindowsFormsApplication1\\WindowsFormsApplication1\\resources\\Sparkle2.png", 0);
            // C:\Users\Anthony\Documents\SGP\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs
            stopwatch.Start();

        }

        public bool Looping = true;

        public void Render()
        {
            Direct3D.Clear((int)BG_Red, (int)BG_Green, (int)BG_Blue);
            Direct3D.DeviceBegin();
            Direct3D.SpriteBegin();

            if (Current_DestBlend != 0)
                Direct3D.Device.RenderState.DestinationBlend = Current_DestBlend;
            if (Current_SourceBlend != 0)
                Direct3D.Device.RenderState.SourceBlend = Current_SourceBlend;

            if (ImageID != -1)
            {
                for (int i = 0; i < Particles.Count - 1; ++i)
                {
                    if (splitContainer1.Panel1.Width > Particles[i].X && splitContainer1.Panel1.Height > Particles[i].Y && Particles[i].X > 0 && Particles[i].Y > 0)
                        TextureManager.Draw(ImageID, (int)Particles[i].X, (int)Particles[i].Y, (float)Particles[i].Current_Scale,
                            (float)Particles[i].Current_Scale, new Rectangle(), 0, 0, 0, Particles[i].Current_Color.ToArgb());
                }
            }

            Direct3D.SpriteEnd();

            Direct3D.DeviceEnd();
            Direct3D.Present();
        }

        public void Update()
        {
            //Environment.TickCount

            fElapsedTime = stopwatch.ElapsedMilliseconds - fLastTime;
            fLastTime = stopwatch.ElapsedMilliseconds;

            switch (EmitterAnimation_Type)
            {
                case 0:
                    break;
                case 1:
                    if (EmitterAnimation_Degree < 360)
                        EmitterAnimation_Degree += (fElapsedTime / 1000);

                    EmitterX += (decimal)MathObject.cos(EmitterAnimation_Degree) * EmitterAnimationVelX;
                    EmitterY += (decimal)MathObject.sin(EmitterAnimation_Degree) * EmitterAnimationVelY;
                    break;
                case 2:
                    EmitterX += EmitterAnimationVelX * (decimal)(fElapsedTime / 1000);
                    EmitterY += EmitterAnimationVelY * (decimal)(fElapsedTime / 1000);
                    break;
            }

            for(int i = 0; i < Particles.Count; ++i)
            {
                Particle temp_particle = new Particle();
                temp_particle = Particles[i];

                if (temp_particle.Current_Scale > temp_particle.End_Scale)
                    temp_particle.Current_Scale -= (temp_particle.Start_Scale / temp_particle.Max_Life) * (decimal)fElapsedTime;
                else
                    temp_particle.Current_Scale += (temp_particle.End_Scale / temp_particle.Max_Life) *(decimal)fElapsedTime;

                int index = ((int)(100 * (temp_particle.Current_Life / temp_particle.Max_Life)));
                if( index >= 100)
                    index = 99;
                else if(index < 0 )
                    index = 0;
                temp_particle.Current_Color = temp_particle.Color_Array[index];
                temp_particle.Current_VelocityX += (temp_particle.Current_AccelerationX * ((decimal)fElapsedTime /1000));
                temp_particle.Current_VelocityY += (temp_particle.Current_AccelerationY * ((decimal)fElapsedTime /1000));
                temp_particle.X += (temp_particle.Current_VelocityX * ((decimal)fElapsedTime/1000));
                temp_particle.Y += (temp_particle.Current_VelocityY * ((decimal)fElapsedTime/1000));
                temp_particle.Current_Life -= (decimal)fElapsedTime;

                if (temp_particle.Current_Life > 0)
                    Particles[i] = temp_particle;
                else
                    Particles.RemoveAt(i);
            }

            if ((checkBox1.Checked || once) && Max_Particles >= Particles.Count)
            {
                Random rand = new Random();
                for (int i = 0; i < Particle_Quanity; ++i)
                {
                    Particle temp_particle = new Particle();
                    int a;

                    switch (EmitterType)
                    {
                        case 0:
                            temp_particle.X = rand.Next(EmitterSize) + EmitterX;
                            temp_particle.Y = rand.Next(EmitterSize) + EmitterY;
                            break;
                        case 1:
                            a = rand.Next(EmitterSize);
                            temp_particle.X = (rand.Next(a) - a / 2) + EmitterX;
                            temp_particle.Y = (decimal)((rand.Next(a) - a / 2) * MathObject.sin(rand.Next(360))) + EmitterY;
                            break;
                        case 2:
                            a = rand.Next(360);
                            temp_particle.X = EmitterSize * (decimal)MathObject.sin(a) + EmitterX;
                            temp_particle.Y = EmitterSize * (decimal)MathObject.cos(a) + EmitterY;
                            break;
                        case 3:
                            a = rand.Next(360);
                            temp_particle.X = EmitterSize * (decimal)(MathObject.sin(a)) + EmitterX;
                            temp_particle.Y = EmitterY;
                            break;
                        case 4:
                            a = rand.Next(360);
                            temp_particle.X = EmitterX;
                            temp_particle.Y = EmitterSize * (decimal)(MathObject.sin(a)) + EmitterY;
                            break;
                    }

                    //int a = rand.Next(360);
                    //temp_particle.X = rand.Next(100) * (decimal)MathObject.sin(a) + EmitterX;
                    //temp_particle.Y = rand.Next(100) * (decimal)MathObject.cos(a) + EmitterY;

                    //temp_particle.X = rand.Next(100) - 50 + EmitterX;
                    // RADIUS (rand.Next(a) - a/2) + EmitterX;
                    //SQUARE rand.Next(EmitterRect.Width) + EmitterRect.X;     //POINT EmitterX;
                    //temp_particle.Y = rand.Next(100) - 50 + EmitterY;
                    // RADIUS (decimal)((rand.Next(a) - a/2)*MathObject.sin(rand.Next(360))) + EmitterY;
                    //SQUARE rand.Next(EmitterRect.Height) + EmitterRect.Y;    //POINT EmitterY;
                    temp_particle.Color_Array = BlendColors(Color.FromArgb((int)End_Alpha, (int)End_Red, (int)End_Green, (int)End_Blue),
                    Color.FromArgb((int)Start_Alpha, (int)Start_Red, (int)Start_Green, (int)Start_Blue), 100);
                    temp_particle.Current_Color = temp_particle.Color_Array[99];
                    temp_particle.Current_Scale = temp_particle.Start_Scale = (decimal)Start_Scale;
                    temp_particle.End_Scale = (decimal)End_Scale;
                    temp_particle.Current_Life = temp_particle.Max_Life = rand.Next((int)(ParticleLife_Max - ParticleLife_Min)) + ParticleLife_Min;
                    temp_particle.Current_VelocityX = rand.Next((int)(ParticleVel_MaxX - ParticleVel_MinX)) + ParticleVel_MinX;
                    temp_particle.Current_VelocityY = rand.Next((int)(ParticleVel_MaxY - ParticleVel_MinY)) + ParticleVel_MinY;
                    temp_particle.Current_AccelerationX = rand.Next((int)(ParticleAccel_MaxX - ParticleAccel_MinX)) + ParticleAccel_MinX;
                    temp_particle.Current_AccelerationY = rand.Next((int)(ParticleAccel_MaxY - ParticleAccel_MinY)) + ParticleAccel_MinY;
                    Particles.Add(temp_particle);
                    once = false;
                }
            }

        }

        private void trackBar16_Scroll(object sender, EventArgs e)
        {
            BG_Red = 255 * ((decimal)trackBar16.Value / (decimal)trackBar16.Maximum);
            numericUpDown15.Value = BG_Red;
        }

        private void numericUpDown15_ValueChanged(object sender, EventArgs e)
        {
            BG_Red = numericUpDown15.Value;
            trackBar16.Value = (int)(25 * (BG_Red / 255));
        }

        private void trackBar15_Scroll(object sender, EventArgs e)
        {
            BG_Green = 255 * ((decimal)trackBar15.Value / (decimal)trackBar15.Maximum);
            numericUpDown16.Value = BG_Green;
        }

        private void numericUpDown16_ValueChanged(object sender, EventArgs e)
        {
            BG_Green = numericUpDown16.Value;
            trackBar15.Value = (int)(25 * (BG_Green / 255));
        }

        private void trackBar17_Scroll(object sender, EventArgs e)
        {
            BG_Blue = 255 * ((decimal)trackBar17.Value / (decimal)trackBar17.Maximum);
            numericUpDown17.Value = BG_Blue;
        }

        private void numericUpDown17_ValueChanged(object sender, EventArgs e)
        {
            BG_Blue = numericUpDown17.Value;
            trackBar17.Value = (int)(25 * (BG_Blue / 255));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            dlg.Color = splitContainer1.Panel1.BackColor;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                splitContainer1.Panel1.BackColor = dlg.Color;
                numericUpDown17.Value = BG_Blue = (decimal)dlg.Color.B;
                numericUpDown15.Value = BG_Red = (decimal)dlg.Color.R;
                numericUpDown16.Value = BG_Green = (decimal)dlg.Color.G;
                trackBar17.Value = (int)(25 * (BG_Blue / 255));
                trackBar15.Value = (int)(25 * (BG_Green / 255));
                trackBar16.Value = (int)(25 * (BG_Red / 255));
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            Start_Alpha = (int)numericUpDown1.Value;
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            Start_Red = (int)numericUpDown4.Value;
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            Start_Green = (int)numericUpDown3.Value;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            Start_Blue = (int)numericUpDown2.Value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            dlg.Color = Color.FromArgb((int)Start_Alpha,(int)Start_Red,(int)Start_Green,(int)Start_Blue);

            if (DialogResult.OK == dlg.ShowDialog())
            {
                Start_Alpha = dlg.Color.A;
                numericUpDown4.Value = Start_Red = dlg.Color.R;
                numericUpDown3.Value = Start_Green = dlg.Color.G;
                numericUpDown2.Value = Start_Blue = dlg.Color.B;
                something_happened = true;
            }
        }

        private void numericUpDown8_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            End_Alpha = (int)numericUpDown8.Value;
        }

        private void numericUpDown6_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            End_Red = (int)numericUpDown6.Value;
        }

        private void numericUpDown7_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            End_Green = (int)numericUpDown7.Value;
        }

        private void numericUpDown5_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            End_Blue = (int)numericUpDown5.Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            dlg.Color = Color.FromArgb((int)End_Alpha,(int)End_Red,(int)End_Green,(int)End_Blue);

            if (DialogResult.OK == dlg.ShowDialog())
            {
                End_Alpha = dlg.Color.A;
                numericUpDown6.Value = End_Red = dlg.Color.R;
                numericUpDown7.Value = End_Green = dlg.Color.G;
                numericUpDown5.Value = End_Blue = dlg.Color.B;
                something_happened = true;
            }
        }

        private void numericUpDown9_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            Start_Scale = (float)numericUpDown9.Value;
        }

        private void numericUpDown10_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            End_Scale = (float)numericUpDown10.Value;
        }

        private void numericUpDown19_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            Particle_Quanity = numericUpDown19.Value;
            trackBar19.Value = (int)(25 * (Particle_Quanity / 500));
        }

        private void trackBar19_Scroll(object sender, EventArgs e)
        {
            something_happened = true;
            Particle_Quanity = 500 * ((decimal)trackBar19.Value / (decimal)trackBar19.Maximum);
            numericUpDown19.Value = (decimal)Particle_Quanity;
        }

        private void numericUpDown21_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleLife_Min = numericUpDown21.Value;
            if (ParticleLife_Min <= 0)
                ParticleLife_Min = 1;

            if (ParticleLife_Min > ParticleLife_Max)
            {
                ParticleLife_Min = ParticleLife_Max;
                if (ParticleLife_Min <= 0)
                    ParticleLife_Min = ParticleLife_Max = 1;
                numericUpDown21.Value = ParticleLife_Min;
            }
        }

        private void numericUpDown26_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleLife_Max = numericUpDown26.Value;
            if (ParticleLife_Max <= 0)
                ParticleLife_Max = 1;
           
            if (ParticleLife_Min > ParticleLife_Max)
            {
                ParticleLife_Max = ParticleLife_Min;
                if (ParticleLife_Max <= 0)
                    ParticleLife_Max = ParticleLife_Min = 1;
                numericUpDown26.Value = ParticleLife_Max;
            }
        }

        private void numericUpDown22_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleVel_MinX = numericUpDown22.Value;

            if(numericUpDown22.Value > numericUpDown23.Value)
            {
                numericUpDown22.Value = numericUpDown23.Value;
                ParticleVel_MinX = numericUpDown22.Value;
            }
        }

        private void numericUpDown23_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleVel_MaxX = numericUpDown23.Value;

            if (numericUpDown22.Value > numericUpDown23.Value)
            {
                numericUpDown23.Value = numericUpDown22.Value;
                ParticleVel_MaxX = numericUpDown23.Value;
            }
        }

        private void numericUpDown29_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleVel_MinY = numericUpDown29.Value;

            if (numericUpDown29.Value > numericUpDown28.Value)
            {
                numericUpDown29.Value = numericUpDown28.Value;
                ParticleVel_MinY = numericUpDown29.Value;
            }
        }

        private void numericUpDown28_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleVel_MaxY = numericUpDown28.Value;

            if (numericUpDown29.Value > numericUpDown28.Value)
            {
                numericUpDown28.Value = numericUpDown29.Value;
                ParticleVel_MaxY = numericUpDown28.Value;
            }
        }

        private void numericUpDown31_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleAccel_MinX = numericUpDown31.Value;

            if (numericUpDown31.Value > numericUpDown30.Value)
            {
                numericUpDown31.Value = numericUpDown30.Value;
                ParticleAccel_MinX = numericUpDown31.Value;
            }
        }

        private void numericUpDown30_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleAccel_MaxX = numericUpDown30.Value;

            if (numericUpDown31.Value > numericUpDown30.Value)
            {
                numericUpDown30.Value = numericUpDown31.Value;
                ParticleAccel_MaxX = numericUpDown30.Value;
            }
        }

        private void numericUpDown25_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleAccel_MinY = numericUpDown25.Value;

            if (numericUpDown25.Value > numericUpDown24.Value)
            {
                numericUpDown25.Value = numericUpDown24.Value;
                ParticleAccel_MinY = numericUpDown25.Value;
            }
        }

        private void numericUpDown24_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            ParticleAccel_MaxY = numericUpDown24.Value;

            if (numericUpDown25.Value > numericUpDown24.Value)
            {
                numericUpDown24.Value = numericUpDown25.Value;
                ParticleAccel_MaxY = numericUpDown24.Value;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            checkBox2.Checked = true;
            checkBox3.Checked = true;
            checkBox4.Checked = true;
            checkBox5.Checked = true;
            checkBox6.Checked = true;
            checkBox7.Checked = true;
            checkBox8.Checked = true;
            checkBox9.Checked = true;
            checkBox10.Checked = true;
            checkBox11.Checked = true;
            checkBox12.Checked = true;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            checkBox5.Checked = false;
            checkBox6.Checked = false;
            checkBox7.Checked = false;
            checkBox8.Checked = false;
            checkBox9.Checked = false;
            checkBox10.Checked = false;
            checkBox11.Checked = false;
            checkBox12.Checked = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            something_happened = true;
            Random rand = new Random();
            once = true;
            if (checkBox2.Checked)
            {
                Start_Alpha = rand.Next(255);
                Start_Red = rand.Next(255);
                Start_Green = rand.Next(255);
                Start_Blue = rand.Next(255);

                numericUpDown1.Value = (int)Start_Alpha;
                numericUpDown4.Value = (int)Start_Red;
                numericUpDown3.Value = (int)Start_Green;
                numericUpDown2.Value = (int)Start_Blue;
            }

            if (checkBox3.Checked)
            {
                End_Alpha = rand.Next(255);
                End_Red = rand.Next(255);
                End_Green = rand.Next(255);
                End_Blue = rand.Next(255);

                numericUpDown8.Value = (int)End_Alpha;
                numericUpDown6.Value = (int)End_Red;
                numericUpDown7.Value = (int)End_Green;
                numericUpDown5.Value = (int)End_Blue;
            }

            if (checkBox9.Checked)
            {
                Start_Scale = (0.01f * (rand.Next(99)+1));
                End_Scale = (0.01f * (rand.Next(99)+1));

                numericUpDown9.Value = (decimal)Start_Scale;
                numericUpDown10.Value = (decimal)End_Scale;
            }

            if (checkBox6.Checked)
            {
                if (!checkBox1.Checked)
                    Particle_Quanity = rand.Next(500);
                else
                    Particle_Quanity = rand.Next(50);

                numericUpDown19.Value = Particle_Quanity;
            }

            if (checkBox5.Checked)
            {
                EmitterType = rand.Next(5);
                switch(EmitterType)
                {
                    case 0:
                        radioButton1.Checked = true;
                        break;
                    case 1:
                        radioButton2.Checked = true;
                        break;
                    case 2:
                        radioButton4.Checked = true;
                        break;
                    case 3:
                        radioButton5.Checked = true;
                        break;
                    case 4:
                        radioButton6.Checked = true;
                        break;
                }
            }

            if (checkBox4.Checked)
            {
                ParticleLife_Min = rand.Next(2499) +1;
                ParticleLife_Max = rand.Next(2499) +1;

                numericUpDown21.Value = ParticleLife_Min;
                numericUpDown26.Value = ParticleLife_Max;
            }

            if (checkBox8.Checked)
            {
                ParticleVel_MinX = rand.Next(2000) - 1000;
                ParticleVel_MaxX = rand.Next(2000) - 1000;
                ParticleVel_MinY = rand.Next(2000) - 1000;
                ParticleVel_MaxY = rand.Next(2000) - 1000;

                numericUpDown22.Value = ParticleVel_MinX;
                numericUpDown23.Value = ParticleVel_MaxX;
                numericUpDown29.Value = ParticleVel_MinY;
                numericUpDown28.Value = ParticleVel_MaxY;
            }

            if (checkBox7.Checked)
            {
                ParticleAccel_MinX = rand.Next(2000) - 1000;
                ParticleAccel_MaxX = rand.Next(2000) - 1000;
                ParticleAccel_MinY = rand.Next(2000) - 1000;
                ParticleAccel_MaxY = rand.Next(2000) - 1000;

                numericUpDown31.Value = ParticleAccel_MinX;
                numericUpDown30.Value = ParticleAccel_MaxX;
                numericUpDown25.Value = ParticleAccel_MinY;
                numericUpDown24.Value = ParticleAccel_MaxY;
            }

            if (checkBox10.Checked)
            {
                comboBox1.SelectedIndex = rand.Next(15);
            }
            if (checkBox11.Checked)
            {
                comboBox2.SelectedIndex = rand.Next(15);
            }

            if (checkBox12.Checked)
            {
                EmitterSize = rand.Next(500);
                numericUpDown18.Value = EmitterSize;
                trackBar18.Value = EmitterSize / 50;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            once = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            something_happened = true;
            EmitterType = 0;
            EmitterSize = 10;
            numericUpDown18.Value = EmitterSize;
            trackBar18.Value = EmitterSize / 50;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            something_happened = true;
            EmitterType = 1;
            EmitterSize = 100;
            numericUpDown18.Value = EmitterSize;
            trackBar18.Value = EmitterSize / 50;
        }
        private Color[] BlendColors(Color start, Color end, int steps)
        {
            Color[] colorPalette = new Color[steps];

            double colorFactor = 100 / System.Convert.ToDouble(steps) / 100;

            for (int colorLevel = 0; colorLevel < steps; colorLevel++)
            {

                double endColorQuantity = (colorLevel + 1) * colorFactor;

                double startColorQuantity = 1 - endColorQuantity;

                colorPalette[colorLevel] = Color.FromArgb(
                Math.Abs(System.Convert.ToInt32(end.A * endColorQuantity + start.A * startColorQuantity)),

                Math.Abs(System.Convert.ToInt32(end.R * endColorQuantity + start.R * startColorQuantity)),

                Math.Abs(System.Convert.ToInt32(end.G * endColorQuantity + start.G * startColorQuantity)),

                Math.Abs(System.Convert.ToInt32(end.B * endColorQuantity + start.B * startColorQuantity)));
            }
            return colorPalette;

        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            something_happened = true;
            EmitterType = 2;
            EmitterSize = 100;
            numericUpDown18.Value = EmitterSize;
            trackBar18.Value = EmitterSize / 50;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All File|*.*|PNG Image|*.png";
            dlg.FilterIndex = 2;
            dlg.DefaultExt = "png";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                ParticleFileName = dlg.FileName;
                ImageID = TextureManager.LoadTexture(ParticleFileName, 0);
                once = true;
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ImageID != -1)
                something_happened = true;
            switch(comboBox1.SelectedIndex)
            {
                case 0:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.BlendFactor;
                    break;
                case 1:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.BothInvSourceAlpha;
                    break;
                case 2:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.BothSourceAlpha;
                    break;
                case 3:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.DestinationAlpha;
                    break;
                case 4:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.DestinationColor;
                    break;
                case 5:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.InvBlendFactor;
                    break;
                case 6:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.InvDestinationAlpha;
                    break;
                case 7:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.InvDestinationColor;
                    break;
                case 8:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.InvSourceAlpha;
                    break;
                case 9:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.InvSourceColor;
                    break;
                case 10:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.One;
                    break;
                case 11:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.SourceAlpha;
                    break;
                case 12:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.SourceAlphaSat;
                    break;
                case 13:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.SourceColor;
                    break;
                case 14:
                    Current_SourceBlend = Microsoft.DirectX.Direct3D.Blend.Zero;
                    break;
            }
            
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ImageID != -1)
                something_happened = true;
            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.BlendFactor;
                    break;
                case 1:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.BothInvSourceAlpha;
                    break;
                case 2:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.BothSourceAlpha;
                    break;
                case 3:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.DestinationAlpha;
                    break;
                case 4:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.DestinationColor;
                    break;
                case 5:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.InvBlendFactor;
                    break;
                case 6:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.InvDestinationAlpha;
                    break;
                case 7:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.InvDestinationColor;
                    break;
                case 8:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.InvSourceAlpha;
                    break;
                case 9:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.InvSourceColor;
                    break;
                case 10:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.One;
                    break;
                case 11:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.SourceAlpha;
                    break;
                case 12:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.SourceAlphaSat;
                    break;
                case 13:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.SourceColor;
                    break;
                case 14:
                    Current_DestBlend = Microsoft.DirectX.Direct3D.Blend.Zero;
                    break;
            }
        }

        private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                EmitterX = OriginalX = e.Location.X;
                EmitterY = OriginalY = e.Location.Y;
                EmitterAnimation_Degree = 0;
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            something_happened = true;
            EmitterType = 3;
            EmitterSize = 100;
            numericUpDown18.Value = EmitterSize;
            trackBar18.Value = EmitterSize / 50;
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            something_happened = true;
            EmitterType = 4;
            EmitterSize = 100;
            numericUpDown18.Value = EmitterSize;
            trackBar18.Value = EmitterSize / 50;
        }

        private void numericUpDown18_ValueChanged(object sender, EventArgs e)
        {
            something_happened = true;
            EmitterSize = (int)numericUpDown18.Value;
            trackBar18.Value = EmitterSize / 50;
        }

        private void trackBar18_Scroll(object sender, EventArgs e)
        {
            something_happened = true;
            EmitterSize = 50 * trackBar18.Value;
            if (EmitterSize <= 0)
                EmitterSize = 1;
            numericUpDown18.Value = EmitterSize;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            EmitterAnimation_Type = 0;
            EmitterX = OriginalX;
            EmitterY = OriginalY;
            EmitterAnimationVelX = numericUpDown20.Value = 0;
            EmitterAnimationVelY = numericUpDown27.Value = 0;
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            EmitterAnimation_Type = 1;
            EmitterX = OriginalX;
            EmitterY = OriginalY;
            EmitterAnimation_Degree = 0;
            EmitterAnimationVelX = numericUpDown20.Value = 5;
            EmitterAnimationVelY = numericUpDown27.Value = 5;
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            EmitterAnimation_Type = 2;
            EmitterX = OriginalX;
            EmitterY = OriginalY;
            EmitterAnimation_Degree = 0;
            EmitterAnimationVelX = numericUpDown20.Value = 100;
            EmitterAnimationVelY = numericUpDown27.Value = 100;
        }

        private void numericUpDown20_ValueChanged(object sender, EventArgs e)
        {
            EmitterAnimationVelX = numericUpDown20.Value;
        }

        private void numericUpDown27_ValueChanged(object sender, EventArgs e)
        {
            EmitterAnimationVelY = numericUpDown20.Value;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml*|Binary Files|*.bin";
            dlg.FilterIndex = 2;
            dlg.DefaultExt = "xml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                if (dlg.FilterIndex == 3)
                {
                    BinaryReader reader = new BinaryReader(dlg.OpenFile(), Encoding.ASCII);

                    Start_Alpha = reader.ReadInt32();
                    numericUpDown1.Value = Start_Alpha;

                    Start_Red = reader.ReadInt32();
                    numericUpDown4.Value = Start_Red;

                    Start_Green = reader.ReadInt32();
                    numericUpDown3.Value = Start_Green;

                    Start_Blue = reader.ReadInt32();
                    numericUpDown2.Value = Start_Blue;


                    End_Alpha = reader.ReadInt32();
                    numericUpDown8.Value = End_Alpha;

                    End_Red = reader.ReadInt32();
                    numericUpDown6.Value = End_Red;

                    End_Green = reader.ReadInt32();
                    numericUpDown7.Value = End_Green;

                    End_Blue = reader.ReadInt32();
                    numericUpDown5.Value = End_Blue;

                    Start_Scale = (float)reader.ReadInt32() / 100.0f;
                    numericUpDown9.Value = (decimal)Start_Scale;
                    End_Scale = (float)reader.ReadInt32() / 100.0f;
                    numericUpDown10.Value = (decimal)End_Scale;

                    ParticleLife_Min = (decimal)reader.ReadInt32();
                    numericUpDown21.Value = ParticleLife_Min;
                    ParticleLife_Max = (decimal)reader.ReadInt32();
                    numericUpDown26.Value = ParticleLife_Max;

                    ParticleVel_MinX = (decimal)reader.ReadInt32();
                    numericUpDown22.Value = ParticleVel_MinX;
                    ParticleVel_MaxX = (decimal)reader.ReadInt32();
                    numericUpDown23.Value = ParticleVel_MaxX;
                    ParticleVel_MinY = (decimal)reader.ReadInt32();
                    numericUpDown29.Value = ParticleVel_MinY;
                    ParticleVel_MaxY = (decimal)reader.ReadInt32();
                    numericUpDown28.Value = ParticleVel_MaxY;

                    ParticleAccel_MinX = (decimal)reader.ReadInt32();
                    numericUpDown31.Value = ParticleAccel_MinX;
                    ParticleAccel_MaxX = (decimal)reader.ReadInt32();
                    numericUpDown30.Value = ParticleAccel_MaxX;
                    ParticleAccel_MinY = (decimal)reader.ReadInt32();
                    numericUpDown25.Value = ParticleAccel_MinY;
                    ParticleAccel_MaxY = (decimal)reader.ReadInt32();
                    numericUpDown24.Value = ParticleAccel_MaxY;

                    Particle_Quanity = (decimal)reader.ReadInt32();
                    numericUpDown19.Value = Particle_Quanity;
                    trackBar19.Value = (int)(25 * (Particle_Quanity / 500));

                    int temp_int = reader.ReadInt32();

                    ParticleFileName = reader.ReadString();

                    if (ParticleFileName != "0")
                    {
                        string temp_string = Environment.CurrentDirectory;
                        temp_string += ParticleFileName;
                        ParticleFileName = temp_string;
                        ImageID = TextureManager.LoadTexture(ParticleFileName, 0);
                    }
                    else
                        ParticleFileName = null;


                    EmitterSize = reader.ReadInt32();
                    numericUpDown18.Value = EmitterSize;
                    trackBar18.Value = EmitterSize / 50;

                    EmitterType = reader.ReadInt32();
                    switch (EmitterType)
                    {
                        case 0:
                            radioButton1.Checked = true;
                            break;
                        case 1:
                            radioButton2.Checked = true;
                            break;
                        case 2:
                            radioButton4.Checked = true;
                            break;
                        case 3:
                            radioButton5.Checked = true;
                            break;
                        case 4:
                            radioButton6.Checked = true;
                            break;
                    }

                    int tempindex = reader.ReadInt32();

                    if (tempindex > -1)
                        comboBox1.SelectedIndex = tempindex;

                    tempindex = reader.ReadInt32();

                    if (tempindex > -1)
                        comboBox2.SelectedIndex = tempindex;

                    checkBox1.Checked =  reader.ReadBoolean();

                    if (!checkBox1.Checked)
                        once = true;

                    reader.Close();
                }
                else if (dlg.FilterIndex == 2)
                {
                    XElement xRoot = XElement.Load(dlg.FileName);
                    XElement xEmitter = xRoot.Element("Emitter");
                    XAttribute xParticleQuanity = xEmitter.Attribute("Quanity");
                    XAttribute xParticleFileName = xEmitter.Attribute("FileName");
                    XAttribute xEmitterSize = xEmitter.Attribute("Size");
                    XAttribute xEmitterType = xEmitter.Attribute("Type");
                    XAttribute xLooping = xEmitter.Attribute("Looping");
                    
                    Particle_Quanity = System.Convert.ToDecimal(xParticleQuanity.Value);
                    numericUpDown19.Value = Particle_Quanity;
                    trackBar19.Value = (int)(25 * (Particle_Quanity / 500));

                    ParticleFileName = xParticleFileName.Value;
                    if (ParticleFileName != "0")
                    {
                        string temp_string = Environment.CurrentDirectory;
                        temp_string += ParticleFileName;
                        ParticleFileName = temp_string;
                        ImageID = TextureManager.LoadTexture(ParticleFileName, 0);
                    }
                    else
                        ParticleFileName = null;

                    EmitterSize = System.Convert.ToInt32(xEmitterSize.Value);
                    numericUpDown18.Value = EmitterSize;
                    trackBar18.Value = EmitterSize / 50;

                    EmitterType = System.Convert.ToInt32(xEmitterType.Value);
                    switch (EmitterType)
                    {
                        case 0:
                            radioButton1.Checked = true;
                            break;
                        case 1:
                            radioButton2.Checked = true;
                            break;
                        case 2:
                            radioButton4.Checked = true;
                            break;
                        case 3:
                            radioButton5.Checked = true;
                            break;
                        case 4:
                            radioButton4.Checked = true;
                            break;
                    }

                    checkBox1.Checked = System.Convert.ToBoolean(xLooping.Value);
                    if (!checkBox1.Checked)
                        once = true;

                    XElement xStartColor = xEmitter.Element("StartColor");
                    XAttribute xStartAlpha = xStartColor.Attribute("Alpha");
                    XAttribute xStartRed = xStartColor.Attribute("Red");
                    XAttribute xStartGreen = xStartColor.Attribute("Green");
                    XAttribute xStartBlue = xStartColor.Attribute("Blue");

                    Start_Alpha = System.Convert.ToInt32(xStartAlpha.Value);
                    numericUpDown1.Value = Start_Alpha;
                    Start_Red = System.Convert.ToInt32(xStartRed.Value);
                    numericUpDown4.Value = Start_Red;
                    Start_Green = System.Convert.ToInt32(xStartGreen.Value);
                    numericUpDown3.Value = Start_Green;                    
                    Start_Blue = System.Convert.ToInt32(xStartBlue.Value);
                    numericUpDown2.Value = Start_Blue;

                    XElement xEndColor = xEmitter.Element("EndColor");
                    XAttribute xEndAlpha = xEndColor.Attribute("Alpha");
                    XAttribute xEndRed = xEndColor.Attribute("Red");
                    XAttribute xEndGreen = xEndColor.Attribute("Green");
                    XAttribute xEndBlue = xEndColor.Attribute("Blue");

                    End_Alpha = System.Convert.ToInt32(xEndAlpha.Value);
                    numericUpDown8.Value = End_Alpha;
                    End_Red = System.Convert.ToInt32(xEndRed.Value);
                    numericUpDown6.Value = End_Red;
                    End_Green = System.Convert.ToInt32(xEndGreen.Value);
                    numericUpDown7.Value = End_Green;
                    End_Blue = System.Convert.ToInt32(xEndBlue.Value);
                    numericUpDown5.Value = End_Blue;

                    XElement xScale = xEmitter.Element("Scale");
                    XAttribute xStart_Scale = xScale.Attribute("Start");
                    XAttribute xEnd_Scale = xScale.Attribute("End");

                    Start_Scale = System.Convert.ToSingle(xStart_Scale.Value);
                    numericUpDown9.Value = (decimal)Start_Scale;                    
                    End_Scale = System.Convert.ToSingle(xEnd_Scale.Value);
                    numericUpDown10.Value = (decimal)End_Scale;

                    XElement xParticleLife = xEmitter.Element("ParticleLife");
                    XAttribute xMin_Life = xParticleLife.Attribute("Min");
                    XAttribute xMax_Life = xParticleLife.Attribute("Max");

                    ParticleLife_Min = System.Convert.ToDecimal(xMin_Life.Value);
                    numericUpDown21.Value = ParticleLife_Min;
                    ParticleLife_Max = System.Convert.ToDecimal(xMax_Life.Value);
                    numericUpDown26.Value = ParticleLife_Max;

                    XElement xParticleVel = xEmitter.Element("ParticleVel");
                    XAttribute xVelX_Min = xParticleVel.Attribute("minX");
                    XAttribute xVelX_Max = xParticleVel.Attribute("maxX");
                    XAttribute xVelY_Min = xParticleVel.Attribute("minY");
                    XAttribute xVelY_Max = xParticleVel.Attribute("maxY");

                    //22 23 29 28
                    ParticleVel_MinX = System.Convert.ToDecimal(xVelX_Min.Value);
                    numericUpDown22.Value = ParticleVel_MinX;
                    ParticleVel_MaxX = System.Convert.ToDecimal(xVelX_Max.Value);
                    numericUpDown23.Value = ParticleVel_MaxX;
                    ParticleVel_MinY = System.Convert.ToDecimal(xVelY_Min.Value);
                    numericUpDown29.Value = ParticleVel_MinY;
                    ParticleVel_MaxY = System.Convert.ToDecimal(xVelY_Max.Value);
                    numericUpDown28.Value = ParticleVel_MaxY;

                    XElement xParticleAccel = xEmitter.Element("ParticleAccel");
                    XAttribute xAccelX_Min = xParticleAccel.Attribute("minX");
                    XAttribute xAccelX_Max = xParticleAccel.Attribute("maxX");
                    XAttribute xAccelY_Min = xParticleAccel.Attribute("minY");
                    XAttribute xAccelY_Max = xParticleAccel.Attribute("maxY");

                    //31 30 25 24
                    ParticleAccel_MinX = System.Convert.ToDecimal(xAccelX_Min.Value);
                    numericUpDown31.Value = ParticleAccel_MinX;
                    ParticleAccel_MaxX = System.Convert.ToDecimal(xAccelX_Max.Value);
                    numericUpDown30.Value = ParticleAccel_MaxX;
                    ParticleAccel_MinY = System.Convert.ToDecimal(xAccelY_Min.Value);
                    numericUpDown25.Value = ParticleAccel_MinY;
                    ParticleAccel_MaxY = System.Convert.ToDecimal(xAccelY_Max.Value);
                    numericUpDown24.Value = ParticleAccel_MaxY;

                    XElement xBlends = xEmitter.Element("Blend");
                    XAttribute xSource = xBlends.Attribute("Source");
                    XAttribute xDest = xBlends.Attribute("Destination");

                    int tempindex = System.Convert.ToInt32(xSource.Value);

                    if (tempindex != -1)
                        comboBox1.SelectedIndex = tempindex;

                    tempindex = System.Convert.ToInt32(xDest.Value);

                    if (tempindex != -1)
                        comboBox2.SelectedIndex = tempindex;
                    //XElement xBlends = new XElement("Blend");
                    //XAttribute xSourceBlend = new XAttribute("Source", comboBox1.SelectedIndex);
                    //XAttribute xDestinationBlend = new XAttribute("Destination", comboBox2.SelectedIndex);
                    //xBlends.Add(xSourceBlend);
                    //xBlends.Add(xDestinationBlend);
                    //xEmitter.Add(xBlends);
                }
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml Files|*.xml*|Binary Files|*.bin";
            dlg.FilterIndex = 1;
            dlg.DefaultExt = "xml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                if (dlg.FilterIndex == 2)
                {
                    BinaryWriter writer = new BinaryWriter(dlg.OpenFile(), Encoding.ASCII);

                    writer.Write(Start_Alpha);
                    writer.Write(Start_Red);
                    writer.Write(Start_Green);
                    writer.Write(Start_Blue);

                    writer.Write(End_Alpha);
                    writer.Write(End_Red);
                    writer.Write(End_Green);
                    writer.Write(End_Blue);

                    writer.Write((int)(Start_Scale*100));
                    writer.Write((int)(End_Scale*100));

                    writer.Write((int)ParticleLife_Min);
                    writer.Write((int)ParticleLife_Max);

                    writer.Write((int)ParticleVel_MinX);
                    writer.Write((int)ParticleVel_MaxX);
                    writer.Write((int)ParticleVel_MinY);
                    writer.Write((int)ParticleVel_MaxY);

                    writer.Write((int)ParticleAccel_MinX);
                    writer.Write((int)ParticleAccel_MaxX);
                    writer.Write((int)ParticleAccel_MinY);
                    writer.Write((int)ParticleAccel_MaxY);

                    writer.Write((int)Particle_Quanity);

                    if (ParticleFileName != null)
                    {
                        string temp_string = null;
                        for (int i = Environment.CurrentDirectory.Length; i < ParticleFileName.Length; ++i)
                        {
                            temp_string += ParticleFileName[i];
                        }
                        writer.Write(temp_string.Length);
                        writer.Write(temp_string);
                    }
                    else
                    {
                        writer.Write(1);
                        writer.Write("0");
                    }

                    writer.Write(EmitterSize);

                    writer.Write(EmitterType);

                    writer.Write(comboBox1.SelectedIndex);
                    writer.Write(comboBox2.SelectedIndex);

                    writer.Write(checkBox1.Checked);

                    writer.Close();
                }
                else if (dlg.FilterIndex == 1)
                {
                    XElement xRoot = new XElement("ParticleED");
                    XElement xEmitter = new XElement("Emitter");
                    XAttribute xParticleQuanity = new XAttribute("Quanity", Particle_Quanity);
                    XAttribute xParticleFileName;
                    if (ParticleFileName != null)
                    {
                        string temp_string = null;
                        for (int i = Environment.CurrentDirectory.Length; i < ParticleFileName.Length; ++i)
                        {
                            temp_string += ParticleFileName[i];
                        }
                        xParticleFileName = new XAttribute("FileName", temp_string);
                    }
                    else
                        xParticleFileName = new XAttribute("FileName", "0");

                    XAttribute xEmitterSize = new XAttribute("Size", EmitterSize);
                    XAttribute xEmitterType = new XAttribute("Type", EmitterType);
                    XAttribute xLooping = new XAttribute("Looping", checkBox1.Checked);
                    xEmitter.Add(xParticleQuanity);
                    xEmitter.Add(xParticleFileName);
                    xEmitter.Add(xEmitterSize);
                    xEmitter.Add(xEmitterType);
                    xEmitter.Add(xLooping);
                    xRoot.Add(xEmitter);

                    XElement xStartColor = new XElement("StartColor");
                    XAttribute xStart_Alpha = new XAttribute("Alpha", Start_Alpha);
                    XAttribute xStart_Red = new XAttribute("Red", Start_Red);
                    XAttribute xStart_Green = new XAttribute("Green", Start_Green);
                    XAttribute xStart_Blue = new XAttribute("Blue", Start_Blue);
                    xStartColor.Add(xStart_Alpha);
                    xStartColor.Add(xStart_Red);
                    xStartColor.Add(xStart_Green);
                    xStartColor.Add(xStart_Blue);
                    xEmitter.Add(xStartColor);

                    XElement xEndColor = new XElement("EndColor");
                    XAttribute xEnd_Alpha = new XAttribute("Alpha", End_Alpha);
                    XAttribute xEnd_Red = new XAttribute("Red", End_Red);
                    XAttribute xEnd_Green = new XAttribute("Green", End_Green);
                    XAttribute xEnd_Blue = new XAttribute("Blue", End_Blue);
                    xEndColor.Add(xEnd_Alpha);
                    xEndColor.Add(xEnd_Red);
                    xEndColor.Add(xEnd_Green);
                    xEndColor.Add(xEnd_Blue);
                    xEmitter.Add(xEndColor);

                    XElement xScale = new XElement("Scale");
                    XAttribute xStart_Scale = new XAttribute("Start", Start_Scale);
                    XAttribute xEnd_Scale = new XAttribute("End", End_Scale);
                    xScale.Add(xStart_Scale);
                    xScale.Add(xEnd_Scale);
                    xEmitter.Add(xScale);

                    XElement xParticleLife = new XElement("ParticleLife");
                    XAttribute xMin_Life = new XAttribute("Min", ParticleLife_Min);
                    XAttribute xMax_Life = new XAttribute("Max", ParticleLife_Max);
                    xParticleLife.Add(xMin_Life);
                    xParticleLife.Add(xMax_Life);
                    xEmitter.Add(xParticleLife);

                    XElement xParticleVel = new XElement("ParticleVel");
                    XAttribute xVelX_Min = new XAttribute("minX", ParticleVel_MinX);
                    XAttribute xVelX_Max = new XAttribute("maxX", ParticleVel_MaxX);
                    XAttribute xVelY_Min = new XAttribute("minY", ParticleVel_MinY);
                    XAttribute xVelY_Max = new XAttribute("maxY", ParticleVel_MaxY);
                    xParticleVel.Add(xVelX_Min);
                    xParticleVel.Add(xVelX_Max);
                    xParticleVel.Add(xVelY_Min);
                    xParticleVel.Add(xVelY_Max);
                    xEmitter.Add(xParticleVel);

                    XElement xParticleAccel = new XElement("ParticleAccel");
                    XAttribute xAccelX_Min = new XAttribute("minX", ParticleAccel_MinX);
                    XAttribute xAccelX_Max = new XAttribute("maxX", ParticleAccel_MaxX);
                    XAttribute xAccelY_Min = new XAttribute("minY", ParticleAccel_MinY);
                    XAttribute xAccelY_Max = new XAttribute("maxY", ParticleAccel_MaxY);
                    xParticleAccel.Add(xAccelX_Min);
                    xParticleAccel.Add(xAccelX_Max);
                    xParticleAccel.Add(xAccelY_Min);
                    xParticleAccel.Add(xAccelY_Max);
                    xEmitter.Add(xParticleAccel);

                    XElement xBlends = new XElement("Blend");
                    XAttribute xSourceBlend = new XAttribute("Source", comboBox1.SelectedIndex);
                    XAttribute xDestinationBlend = new XAttribute("Destination", comboBox2.SelectedIndex);
                    xBlends.Add(xSourceBlend);
                    xBlends.Add(xDestinationBlend);
                    xEmitter.Add(xBlends);

                    xRoot.Save(dlg.FileName);
                }
                something_happened = false;
            }
        }

        private void splitContainer1_Panel1_MouseClick(object sender, MouseEventArgs e)
        {
            EmitterX = OriginalX = e.Location.X;
            EmitterY = OriginalY = e.Location.Y;
            EmitterAnimation_Degree = 0;
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ImageID != -1)
            {
                TextureManager.ReleaseTexture(ImageID);
                ImageID = -1;
            }

            numericUpDown1.Value = 255;
            numericUpDown4.Value = 0;
            numericUpDown3.Value = 255;
            numericUpDown2.Value = 0;

            numericUpDown8.Value = 255;
            numericUpDown6.Value = 0;
            numericUpDown7.Value = 0;
            numericUpDown5.Value = 0;

            numericUpDown9.Value = (decimal)0.25;
            numericUpDown10.Value = (decimal)0.75;

            numericUpDown21.Value = 100;
            numericUpDown26.Value = 1000;

            numericUpDown22.Value = -10;
            numericUpDown23.Value = 10;
            numericUpDown29.Value = -10;
            numericUpDown28.Value = 10;

            numericUpDown31.Value = -1;
            numericUpDown30.Value = 1;
            numericUpDown25.Value = -1;
            numericUpDown24.Value = 1;

            numericUpDown19.Value = 64;
            numericUpDown18.Value = 10;

            radioButton1.Checked = true;

            comboBox1.SelectedIndex = 11;
            comboBox2.SelectedIndex = 8;

            radioButton3.Checked = true;
            numericUpDown20.Value = 0;
            numericUpDown27.Value = 0;

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (something_happened)
            {
                string messageBoxText = "Do you want to save changes?";
                string caption = "ParticleED";
                DialogResult result = MessageBox.Show(messageBoxText, caption,
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (result == DialogResult.Yes)
                {
                    saveAsToolStripMenuItem_Click(saveAsToolStripMenuItem, new EventArgs());
                    Looping = false;
                    this.Close();
                }
                else if (result == DialogResult.No)
                {
                    Looping = false;
                    this.Close();
                }
                else if (result == DialogResult.Cancel)
                {
                }
            }
            else
            {
                Looping = false;
                this.Close();
            }
        }
    }
}
